home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Caml Light 0.61 / Source / src / yacc / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-24  |  7.1 KB  |  383 lines  |  [TEXT/MPS ]

  1. #include <signal.h>
  2. #include "defs.h"
  3.  
  4. char dflag;
  5. char lflag;
  6. char rflag;
  7. char tflag;
  8. char vflag;
  9. char sflag;
  10. char big_endian;
  11.  
  12. char *file_prefix = 0;
  13. char *myname = "yacc";
  14. #ifdef NO_UNIX
  15. char temp_form[] = "yacc.X";
  16. #else
  17. char temp_form[] = "yacc.XXXXXXX";
  18. #endif
  19.  
  20. int lineno;
  21. int outline;
  22.  
  23. char *action_file_name;
  24. char *entry_file_name;
  25. char *code_file_name;
  26. char *interface_file_name;
  27. char *defines_file_name;
  28. char *input_file_name = "";
  29. char *output_file_name;
  30. char *text_file_name;
  31. char *union_file_name;
  32. char *verbose_file_name;
  33.  
  34. FILE *action_file;    /*  a temp file, used to save actions associated    */
  35.             /*  with rules until the parser is written        */
  36. FILE *entry_file;
  37. FILE *code_file;    /*  y.code.c (used when the -r option is specified) */
  38. FILE *defines_file;    /*  y.tab.h                        */
  39. FILE *input_file;    /*  the input file                    */
  40. FILE *output_file;    /*  y.tab.c                        */
  41. FILE *text_file;    /*  a temp file, used to save text until all        */
  42.             /*  symbols have been defined                */
  43. FILE *union_file;    /*  a temp file, used to save the union            */
  44.             /*  definition until all symbol have been        */
  45.             /*  defined                        */
  46. FILE *verbose_file;    /*  y.output                        */
  47. FILE *interface_file;
  48.  
  49. int nitems;
  50. int nrules;
  51. int ntotalrules;
  52. int nsyms;
  53. int ntokens;
  54. int nvars;
  55.  
  56. int   start_symbol;
  57. char  **symbol_name;
  58. short *symbol_value;
  59. short *symbol_prec;
  60. char  *symbol_assoc;
  61. char **symbol_tag;
  62. char *symbol_true_token;
  63.  
  64. short *ritem;
  65. short *rlhs;
  66. short *rrhs;
  67. short *rprec;
  68. char  *rassoc;
  69. short **derives;
  70. char *nullable;
  71.  
  72. extern char *mktemp();
  73. extern char *getenv();
  74.  
  75.  
  76. done(k)
  77. int k;
  78. {
  79.     if (action_file) { fclose(action_file); unlink(action_file_name); }
  80.     if (entry_file) { fclose(entry_file); unlink(entry_file_name); }
  81.     if (text_file) { fclose(text_file); unlink(text_file_name); }
  82.     if (union_file) { fclose(union_file); unlink(union_file_name); }
  83.     exit(k);
  84. }
  85.  
  86.  
  87. void onintr(dummy)
  88.      int dummy;
  89. {
  90.     done(1);
  91. }
  92.  
  93.  
  94. set_signals()
  95. {
  96. #ifdef SIGINT
  97.     if (signal(SIGINT, SIG_IGN) != SIG_IGN)
  98.     signal(SIGINT, onintr);
  99. #endif
  100. #ifdef SIGTERM
  101.     if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
  102.     signal(SIGTERM, onintr);
  103. #endif
  104. #ifdef SIGHUP
  105.     if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
  106.     signal(SIGHUP, onintr);
  107. #endif
  108. }
  109.  
  110.  
  111. usage()
  112. {
  113.     fprintf(stderr, "usage: %s [-vs] [-b file_prefix] [-el|-eb] filename\n",
  114.             myname);
  115.     exit(1);
  116. }
  117.  
  118. getargs(argc, argv)
  119. int argc;
  120. char *argv[];
  121. {
  122.     register int i;
  123.     register char *s;
  124.  
  125.     if (argc > 0) myname = argv[0];
  126.     for (i = 1; i < argc; ++i)
  127.     {
  128.     s = argv[i];
  129.     if (*s != '-') break;
  130.     switch (*++s)
  131.     {
  132.     case '\0':
  133.         input_file = stdin;
  134.         if (i + 1 < argc) usage();
  135.         return;
  136.  
  137.     case '-':
  138.         ++i;
  139.         goto no_more_options;
  140.  
  141.     case 'b':
  142.         if (*++s)
  143.          file_prefix = s;
  144.         else if (++i < argc)
  145.         file_prefix = argv[i];
  146.         else
  147.         usage();
  148.         continue;
  149.  
  150.     case 'v':
  151.         vflag = 1;
  152.         break;
  153.  
  154.     case 's':
  155.         sflag = 1;
  156.         break;
  157.  
  158.     default:
  159.         usage();
  160.     }
  161.  
  162.     for (;;)
  163.     {
  164.         switch (*++s)
  165.         {
  166.         case '\0':
  167.         goto end_of_option;
  168.  
  169.         case 'v':
  170.         vflag = 1;
  171.         break;
  172.  
  173.         case 's':
  174.         sflag = 1;
  175.         break;
  176.  
  177.         default:
  178.         usage();
  179.         }
  180.     }
  181. end_of_option:;
  182.     }
  183.  
  184. no_more_options:;
  185.     if (i + 1 != argc) usage();
  186.     input_file_name = argv[i];
  187.     if (file_prefix == 0) {
  188.       int len;
  189.       len = strlen(argv[i]);
  190.       file_prefix = malloc(len + 1);
  191.       if (file_prefix == 0) no_space();
  192.       strcpy(file_prefix, argv[i]);
  193.       while (len > 0) {
  194.         len--;
  195.         if (file_prefix[len] == '.') {
  196.           file_prefix[len] = 0;
  197.           break;
  198.         }
  199.       }
  200.     }
  201. }
  202.  
  203.  
  204. char *
  205. allocate(n)
  206. unsigned n;
  207. {
  208.     register char *p;
  209.  
  210.     p = NULL;
  211.     if (n)
  212.     {
  213.     p = CALLOC(1, n);
  214.     if (!p) no_space();
  215.     }
  216.     return (p);
  217. }
  218.  
  219.  
  220. create_file_names()
  221. {
  222.     int i, len;
  223.     char *tmpdir;
  224.  
  225. #ifdef NO_UNIX
  226.     len = 0;
  227.     i = sizeof(temp_form);
  228. #else
  229.     tmpdir = getenv("TMPDIR");
  230.     if (tmpdir == 0) tmpdir = "/tmp";
  231.     len = strlen(tmpdir);
  232.     i = len + sizeof(temp_form);
  233.     if (len && tmpdir[len-1] != '/')
  234.     ++i;
  235. #endif
  236.  
  237.     action_file_name = MALLOC(i);
  238.     if (action_file_name == 0) no_space();
  239.     entry_file_name = MALLOC(i);
  240.     if (entry_file_name == 0) no_space();
  241.     text_file_name = MALLOC(i);
  242.     if (text_file_name == 0) no_space();
  243.     union_file_name = MALLOC(i);
  244.     if (union_file_name == 0) no_space();
  245.  
  246. #ifndef NO_UNIX
  247.     strcpy(action_file_name, tmpdir);
  248.     strcpy(entry_file_name, tmpdir);
  249.     strcpy(text_file_name, tmpdir);
  250.     strcpy(union_file_name, tmpdir);
  251.  
  252.     if (len && tmpdir[len - 1] != '/')
  253.     {
  254.     action_file_name[len] = '/';
  255.     entry_file_name[len] = '/';
  256.     text_file_name[len] = '/';
  257.     union_file_name[len] = '/';
  258.     ++len;
  259.     }
  260. #endif
  261.  
  262.     strcpy(action_file_name + len, temp_form);
  263.     strcpy(entry_file_name + len, temp_form);
  264.     strcpy(text_file_name + len, temp_form);
  265.     strcpy(union_file_name + len, temp_form);
  266.  
  267.     action_file_name[len + 5] = 'a';
  268.     entry_file_name[len + 5] = 'e';
  269.     text_file_name[len + 5] = 't';
  270.     union_file_name[len + 5] = 'u';
  271.  
  272. #ifndef NO_UNIX
  273.     mktemp(action_file_name);
  274.     mktemp(entry_file_name);
  275.     mktemp(text_file_name);
  276.     mktemp(union_file_name);
  277. #endif
  278.  
  279.     len = strlen(file_prefix);
  280.  
  281.     output_file_name = MALLOC(len + 7);
  282.     if (output_file_name == 0)
  283.     no_space();
  284.     strcpy(output_file_name, file_prefix);
  285.     strcpy(output_file_name + len, OUTPUT_SUFFIX);
  286.  
  287.     code_file_name = output_file_name;
  288.  
  289.     if (vflag)
  290.     {
  291.     verbose_file_name = MALLOC(len + 8);
  292.     if (verbose_file_name == 0)
  293.         no_space();
  294.     strcpy(verbose_file_name, file_prefix);
  295.     strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
  296.     }
  297.  
  298.     interface_file_name = MALLOC(len + 8);
  299.     if (interface_file_name == 0)
  300.     no_space();
  301.     strcpy(interface_file_name, file_prefix);
  302.     strcpy(interface_file_name + len, INTERFACE_SUFFIX);
  303.  
  304. }
  305.  
  306.  
  307. open_files()
  308. {
  309.     create_file_names();
  310.  
  311.     if (input_file == 0)
  312.     {
  313.     input_file = fopen(input_file_name, "r");
  314.     if (input_file == 0)
  315.         open_error(input_file_name);
  316.     }
  317.  
  318.     action_file = fopen(action_file_name, "w");
  319.     if (action_file == 0)
  320.     open_error(action_file_name);
  321.  
  322.     entry_file = fopen(entry_file_name, "w");
  323.     if (entry_file == 0)
  324.     open_error(entry_file_name);
  325.  
  326.     text_file = fopen(text_file_name, "w");
  327.     if (text_file == 0)
  328.     open_error(text_file_name);
  329.  
  330.     if (vflag)
  331.     {
  332.     verbose_file = fopen(verbose_file_name, "w");
  333.     if (verbose_file == 0)
  334.         open_error(verbose_file_name);
  335.     }
  336.  
  337.     if (dflag)
  338.     {
  339.     defines_file = fopen(defines_file_name, "w");
  340.     if (defines_file == 0)
  341.         open_error(defines_file_name);
  342.     union_file = fopen(union_file_name, "w");
  343.     if (union_file ==  0)
  344.         open_error(union_file_name);
  345.     }
  346.  
  347.     output_file = fopen(output_file_name, "w");
  348.     if (output_file == 0)
  349.     open_error(output_file_name);
  350.  
  351.     if (rflag)
  352.     {
  353.     code_file = fopen(code_file_name, "w");
  354.     if (code_file == 0)
  355.         open_error(code_file_name);
  356.     }
  357.     else
  358.     code_file = output_file;
  359.  
  360.  
  361.     interface_file = fopen(interface_file_name, "w");
  362.     if (interface_file == 0)
  363.       open_error(interface_file_name);
  364. }
  365.  
  366.  
  367. main(argc, argv)
  368. int argc;
  369. char *argv[];
  370. {
  371.     set_signals();
  372.     getargs(argc, argv);
  373.     open_files();
  374.     reader();
  375.     lr0();
  376.     lalr();
  377.     make_parser();
  378.     verbose();
  379.     output();
  380.     done(0);
  381.     /*NOTREACHED*/
  382. }
  383.